import { getSystemUserList } from '@/api/system/userConfig.js'
import { getSystemRoleInsertPower } from '@/api/system/roleConfig.js'
import { getSystemPowerList } from '@/api/system/powerConfig.js'

// util
import { handleRequestStatus } from '@/utils/uitls.js'
import { cloneDeep } from '@/utils/uitls.js'
import { clearUserInfo } from '@/views/login/service'

export default {
  methods: {
    async getSystemUserList(params) {
      const res = await getSystemUserList(params)
      // console.log('getSystemUserList', res)
      if (res.data.status !== 0) {
        this.userList = []
        this.userNodata = true
        return
      }
      const data = res.data.data || []
      if (!data.length) {
        this.userList = []
        this.userNodata = true
        return
      }
      this.userList = Object.freeze(data)
      this.handleUserClick(this.userList[0], 0)
      this.userNodata = false
    },

    async getSystemPowerList(params) {
      const res = await getSystemPowerList(params)
      // console.log('getSystemPowerList', res)
      if (res.data.status !== 0) {
        this.treeData = []
        this.treeDataNodata = true
        return
      }
      const data = res.data.data || []
      if (!data.length) {
        this.treeData = []
        this.treeDataNodata = true
        this.authorityModel = false
        return
      }

      // 是否有选中项
      let checkItem
      for (let i = 0, len = data.length; i < len; i++) {
        // 设置根节点
        this.setNodesRootId(data[i], data[i].powerId)
        const isCheck = this.nodesIsChecked(data[i])
        if (isCheck) {
          checkItem = data[i]
        }
      }

      // 设置节点状态
      for (let i = 0, len = data.length; i < len; i++) {
        if (!checkItem) break // 没有选中项不进行设置
        if (checkItem === data[i]) {
        } else {
          this.setNodeState(data[i], true)
        }
      }

      this.treeData = data
      this.treeData_backup = cloneDeep(data)
      this.treeDataNodata = false
    },

    async getSystemRoleInsertPower(params) {
      const res = await getSystemRoleInsertPower(params)
      handleRequestStatus(res, '保存', () => {
        // clearUserInfo()
        this.requestUserList()
        const stateTree = this.$refs['ref_tree'].stateTree
        this.treeData_backup = cloneDeep(stateTree)
      })
    },

    setNodesRootId(node, id) {
      node.custom_rootId = id
      if (Array.isArray(node.children) && node.children.length) {
        node.children.forEach((it) => this.setNodesRootId(it, id))
      }
    },

    nodesIsChecked(node) {
      if (!node) return false
      if (node.checked) return true
      if (Array.isArray(node.children) && node.children.length) {
        for (let i = 0, len = node.children.length; i < len; i++) {
          if (this.nodesIsChecked(node.children[i])) {
            return true
          }
        }
      }
      return false
    },

    setNodeState(node, state) {
      node.disableCheckbox = state
      if (Array.isArray(node.children) && node.children.length) {
        for (let i = 0, len = node.children.length; i < len; i++) {
          this.setNodeState(node.children[i], state)
        }
      }
    },
  },
}
